package model

import "time"

type Match struct {
	Id int64 `xorm:"pk autoincr"`
	Season string
	CompetitionDate string
	CompetitionTime string
	HomeTeamId int64
	HomeTeamName string
	VisitingTeamId int64
	VisitingTeamName string
	Score string
	Start bool
	TeamSectionDataList []*TeamSectionData `xorm:"-"`
	GameTeamDataList []*GameTeamData `xorm:"-"`
	GamePlayerDataList []*GamePlayerData `xorm:"-"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type CompentitionVideo struct {
	Id int64 `xorm:"pk autoincr"`
	MatchId int64
	Type int
	TypeName string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type TheBest struct {
	MatchId int64
	TeamId int64
	TeamName string
	Score *GamePlayerData
	Assists *GamePlayerData
	Rebounds *GamePlayerData
}

type GamePlayerData struct {
	Id int64 `xorm:"pk autoincr"`
	PlayerId int64
	PlayerName string
	TeamId int64
	TeamName string
	MatchId int64
	DeliveryTime int
	Score float64
	Assists float64
	Rebounds float64
	Field float64
	Block float64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type GameTeamData struct {
	Id int64 `xorm:"pk autoincr"`
	TeamId int64
	MatchId int64
	TeamName string
	Score float64
	Assists float64
	Rebounds float64
	Field float64
	Block float64
	ShootingPercentage float64
	FreeThrowPercentage float64
	ThreePointPercentage float64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type TeamSectionData struct {
	Id int64 `xorm:"pk autoincr"`
	MatchId int64
	TeamId int64
	TeamName string
	Section1 int
	Section2 int
	Section3 int
	Section4 int
	Sum int
	CreatedAt time.Time
	UpdatedAt time.Time
}

type AllianceArea struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	SeasonTeamDataList []*SeasonTeamData `xorm:"-"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Team struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	EstablishmentDate string
	PlayerList []*Player `xorm:"-"`
	AllianceAreaId int64
	AllianceAreaName string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Player struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	TeamId int64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type SeasonPlayerData struct {
	Id int64 `xorm:"pk autoincr"`
	Season string
	PlayerId int64
	PlayerName string
	AverageScore float64
	AverageAssists float64
	AverageRebounds float64
	AverageField float64
	AverageBlock float64
	ShootingPercentage float64
	FreeThrowPercentage float64
	ThreePointPercentage float64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type SeasonTeamData struct {
	Id int64 `xorm:"pk autoincr"`
	Season string
	TeamId int64
	TeamName string
	AverageScore float64
	AverageAssists float64
	AverageRebounds float64
	AverageField float64
	AverageBlock float64
	ShootingPercentage float64
	FreeThrowPercentage float64
	ThreePointPercentage float64
	Victories int
	Faileds int
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (match *Match)GenerateMovieTickets(cd string, ct string, ht *Team, vt *Team, s string)(map[string]interface{}, error){

	var m = Match{
		Season:          s,
		CompetitionDate : cd,
		CompetitionTime : ct,
		HomeTeamId : ht.Id,
		HomeTeamName : ht.Name,
		VisitingTeamId : vt.Id,
		VisitingTeamName : vt.Name,
		Score : "000 - 000",
		Start: false,
		CreatedAt : time.Now(),
		UpdatedAt : time.Now(),
	}
	x.Insert(&m)

	return map[string]interface{}{"message":"成功", "code":200}, nil
}

func (match *Match)QuerySchedule(s string)([]*Match, error){

	var mList []*Match
	x.Where("season = ?", s).Find(&mList)

	return mList, nil
}

func (match *Match)Ranking(s string)( []*AllianceArea, error){

	var aaList []*AllianceArea
	x.Find(&aaList)
	for _, aa :=range aaList{
		x.Join("INNER", "team", "team.id = season_team_data.team_id").
			Join("INNER", "alliance_area", "team.alliance_area_id = alliance_area.id").
			Where("alliance_area.id = ?", aa.Id).
			And("season_team_data.season = ?", s).
			Desc("season_team_data.victories").
			Find(&aa.SeasonTeamDataList)
	}

	return aaList, nil
}

func (match *Match)PlayerList(s string, t int)([]*SeasonPlayerData, error){

	var skill string
	if t == 1{
		skill = "average_score"
	}else if t == 2{
		skill = "average_assists"
	}else if t == 3{
		skill = "average_rebounds"
	}else if t == 4{
		skill = "average_field"
	}else if t == 5{
		skill = "average_block"
	}

	var spdList []*SeasonPlayerData
	x.Where("season = ?", s).Desc(skill).Limit(20).Find(&spdList)

	return spdList, nil
}

func (match *Match)TeamList(s string, t int)([]*SeasonTeamData, error){

	var skill string
	if t == 1{
		skill = "average_score"
	}else if t == 2{
		skill = "average_assists"
	}else if t == 3{
		skill = "average_rebounds"
	}else if t == 4{
		skill = "average_field"
	}else if t == 5{
		skill = "average_block"
	}else if t == 6{
		skill = "shooting_percentage"
	}else if t == 7{
		skill = "free_throw_percentage"
	}else if t == 8{
		skill = "three_point_percentage"
	}

	var stdList []*SeasonTeamData
	x.Where("season = ?", s).Desc(skill).Limit(20).Find(&stdList)

	return stdList, nil

}

func (match *Match)QueryTheBest(mId int64)([]*TheBest, error){

	var m Match
	x.Id(mId).Get(&m)

	var tbList []*TheBest

	var hgpdScore GamePlayerData
	x.Where("match_id = ?", m.Id).And("team_id = ?", m.HomeTeamId).Desc("score").Limit(1).Get(&hgpdScore)

	var hgpdAssists GamePlayerData
	x.Where("match_id = ?", m.Id).And("team_id = ?", m.HomeTeamId).Desc("assists").Limit(1).Get(&hgpdAssists)

	var hgpdRebounds GamePlayerData
	x.Where("match_id = ?", m.Id).And("team_id = ?", m.HomeTeamId).Desc("Rebounds").Limit(1).Get(&hgpdRebounds)
	var htb = TheBest{
		MatchId:  m.Id,
		TeamId:   m.HomeTeamId,
		TeamName: m.HomeTeamName,
		Score:    &hgpdScore,
		Assists:  &hgpdAssists,
		Rebounds: &hgpdRebounds,
	}
	tbList = append(tbList, &htb)

	var vgpdScore GamePlayerData
	x.Where("match_id = ?", m.Id).And("team_id = ?", m.HomeTeamId).Desc("score").Limit(1).Get(&vgpdScore)

	var vgpdAssists GamePlayerData
	x.Where("match_id = ?", m.Id).And("team_id = ?", m.HomeTeamId).Desc("assists").Limit(1).Get(&vgpdAssists)

	var vgpdRebounds GamePlayerData
	x.Where("match_id = ?", m.Id).And("team_id = ?", m.HomeTeamId).Desc("Rebounds").Limit(1).Get(&vgpdRebounds)
	var vtb = TheBest{
		MatchId:  m.HomeTeamId,
		TeamId:   m.VisitingTeamId,
		TeamName: m.VisitingTeamName,
		Score:    &vgpdScore,
		Assists:  &vgpdAssists,
		Rebounds: &vgpdRebounds,
	}
	tbList = append(tbList, &vtb)

	return tbList, nil
}













